/**
 * 
 */
package org.HisCanvas.ChrisRiffle.JFlightLog.swingUi.table;

import java.util.List;

import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.AbstractTableModel;

import org.HisCanvas.ChrisRiffle.JFlightLog.common.LogBookRecUtils;
import org.HisCanvas.ChrisRiffle.JFlightLog.dataModel.LogBook;
import org.HisCanvas.ChrisRiffle.JFlightLog.launching.JFlightLogController;
import org.HisCanvas.ChrisRiffle.JFlightLog.logging.LogController;
import org.HisCanvas.ChrisRiffle.JFlightLog.persist.LogBookDAO;

/**
 * @author RIF
 *
 */
public class FlightLogTableModelSetterWorker extends SwingWorker<AbstractTableModel, Void> {

	
	private JTable logBookTable;
	
	private List<String> orderedColumnNameList;

	/**
	 * constructor
	 * @param pTableToSetModelInto
	 */
	public FlightLogTableModelSetterWorker(JTable pTableToSetModelInto, 
			List<String> pOrderedColumnNameList) {
			
		logBookTable = pTableToSetModelInto;
		
		orderedColumnNameList = pOrderedColumnNameList;
	}
	
	
	
	/* (non-Javadoc)
	 * @see javax.swing.SwingWorker#doInBackground()
	 */
	@Override
	protected AbstractTableModel doInBackground() throws Exception {
		
		
		LogBookDAO logBookDao = JFlightLogController.instance().getLogBookDao();
		
		LogBook logBook = logBookDao.getLogBook();
		
		return new FlightLogTableModel(logBook.getLogBookRecList(), orderedColumnNameList);
	}
	
	
	
	@Override
	protected void done() {
		
		AbstractTableModel tableModel = null;
		
		try{
			tableModel = get();
			
			logBookTable.setModel(tableModel);
			
			//just let the last column(remarks) grow larger
			logBookTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			
	        for (int c=0; c<logBookTable.getColumnCount(); c++) {
	        	TableUtils.packColumn(logBookTable, c);
	        }
	        	     
	        
	        // set all the flight hour 'float columns" to just show one decimal tenth place
	        for(String iFloatColumn : LogBookRecUtils.HOURS_FLOATS_COLUMN_LIST){
	        	
	        	//get the 'short name'(jtable col display name) for this float column
	        	String tableShortColName = LogBookRecUtils.getFieldShortName(iFloatColumn);	
	        	
	        	//set the custom column rederer to display the the decimal value with one decimal digit (1.10 => 1.1) 
	        	logBookTable.getColumn(tableShortColName).setCellRenderer(FlightTableHoursDecimalCellRenderer.instance());	        
	        }
	               
	        
        	//get the 'short name'(jtable col display name) for this DATE column
        	String tableShortColName = LogBookRecUtils.getFieldShortName(LogBookRecUtils.DATE_OF_FLIGHT);	
        	
        	//set the custom column rederer to display the the DATE as mm/dd/yyyy
        	logBookTable.getColumn(tableShortColName).setCellRenderer(FlightTableDateCellRenderer.instance());
        	
        	
        	//data in the 'date' column will be longer/wider than the 'dat' text
        	tableShortColName = LogBookRecUtils.getFieldShortName(LogBookRecUtils.DATE_OF_FLIGHT);	
    		int dateColumnIndex = logBookTable.getColumnModel().getColumnIndex(tableShortColName);
    		logBookTable.getColumnModel().getColumn(dateColumnIndex).setPreferredWidth(60);
    		logBookTable.getColumnModel().getColumn(dateColumnIndex).setMinWidth(60);

    		
    		
        	tableShortColName = LogBookRecUtils.getFieldShortName(LogBookRecUtils.AIRCRAFT_REGISTRATION);	
    		int registrationIndex = logBookTable.getColumnModel().getColumnIndex(tableShortColName);
    		logBookTable.getColumnModel().getColumn(registrationIndex).setPreferredWidth(50);
    		logBookTable.getColumnModel().getColumn(registrationIndex).setMinWidth(50);
    		
    		
    		
    		//data in the remarks column will be longer than the "rmks" text
    		tableShortColName = LogBookRecUtils.getFieldShortName(LogBookRecUtils.REMARKS);
    		int remarksColumnIndex = logBookTable.getColumnModel().getColumnIndex(tableShortColName);
    		logBookTable.getColumnModel().getColumn(remarksColumnIndex).setPreferredWidth(100);
	        
	        

			
		}catch (Exception e) {
			
			LogController.severe(e.getMessage());
			e.printStackTrace();
		}
		
		
	}

}





